/*

  The contents of this file are subject to the Mozilla Public License Version
  1.1 (the "License"); you may not use this file except in compliance with
  the License. You may obtain a copy of the License at 
  
           http://www.mozilla.org/MPL/ 
  
  Software distributed under the License is distributed on an "AS IS" basis,
  WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
  for the specific language governing rights and limitations under the License. 
  
  The Original Code is KazeOS Framework.
  
  The Initial Developer of the Original Code is
  ALCARAZ Marc (aka eKameleon) <ekameleon@gmail.com>.
  Portions created by the Initial Developer are Copyright (C) 2004-2010
  the Initial Developer. All Rights Reserved.
  
  Contributor(s) :
  
*/

package evisio.states.authentification 
{
    import evisio.display.DisplayList;
    import evisio.states.State;
    import evisio.states.core.CoreState;

    import lunas.components.textinputs.VisualTextInput;

    import system.Strings;
    import system.numeric.Range;

    import vegas.display.ApplicationDisplay;
    import vegas.net.NetServerConnection;

    import flash.display.DisplayObject;
    import flash.events.Event;

    /**
     * The authentification state.
     */
    public class AuthentificationState extends CoreState implements State
    {
        
        /**
         * Creates a new AuthentificationState instance.
         */
        public function AuthentificationState()
        {
            super() ;
        }        
        
        /**
         * The connection of the rtmp application.
         */
        public var connection:NetServerConnection ;
        
        /**
         * The name textinput reference.
         */
        public var nameInput:VisualTextInput ;
        
        /**
         * The name String length range.
         */
        public var nameRange:Range = new Range(3, 15) ;        
        
        /**
         * The host of the rtmp server.
         */
        public var rtmpHost:String ;
        
        /**
         * The name of the rtmpHost flashvars.
         */
        public var rtmpHostName:String = "rtmpHost" ;
        
        /**
         * The url pattern of the rtmp server.
         */
        public var rtmpUrl:String ;
        
        /**
         * The name of the rtmpUrl flashvars.
         */
        public var rtmpUrlName:String = "rtmpUrl" ;
                
        /**
         * Invoked to launch the connection.
         */
        public function connect( e:Event = null ):void
        {
            var name:String ;
            
            if ( nameInput != null )
            {
                name = nameInput.text ;
            }
            
            if ( connection != null )
            {
               if ( name != null && nameRange != null && ! nameRange.isOutOfRange( name.length ) ) 
               {
                       if( ApplicationDisplay.flashVars.contains( rtmpHostName ) )
                       {
                           rtmpHost = ApplicationDisplay.flashVars.getValue( rtmpHostName ) ;
                       }
                       
                    if( ApplicationDisplay.flashVars.contains( rtmpUrlName ) )
                    {
                        rtmpUrl = ApplicationDisplay.flashVars.getValue( rtmpUrlName ) ;
                    }
                       
                    var url:String = Strings.format( rtmpUrl , rtmpHost ) ;
                    
                    logger.debug(this + " connect name:" + name + " url:" + url ) ;
                    
                    message = getMessage("connection_in_progress") ; 
                    
                    connection.connect( url , name ) ;
               }
               else
               {
                       message = Strings.format( getMessage("connection_pseudo_invalid") , nameRange.min , nameRange.max )  ; 
                    logger.warn(this + " connect failed, the specified name String must 3 or more letters.") ;
               }
            }
            else
            {
                message = getMessage("connection_invalid") ; 
                logger.warn(this + " connect failed, the connection reference is null.") ;
            }
            
        }
        
        /**
         * Open the state.
         */
        public override function open():void
        {
            if ( factory != null )
            {
                var bt:DisplayObject = factory.getObject( DisplayList.DISCONNECT_BUTTON ) as DisplayObject ;
                if ( bt != null )
                {
                    bt.visible = false  ;
                }
            }
        }
        
        /**
         * Invoked to reset the state.
         */
        public override function reset( e:Event = null ):void
        {
            super.reset() ;
            if ( nameInput != null )
            {
                nameInput.text = "" ;
            } 
        }
    }
}
